package com.fit.spring.boot.proxy.interceptor.extension;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInvocation;

/**
 * 拦截执行器
 * @author hcl
 *
 */
public class ProxyExtensionMethodInvocation implements MethodInvocation{
	/**
	 * 拦截器链
	 */
	private InterceptorChain interceptorChain;
	/**
	 * 执行器
	 */
	private MethodInvocation methodInvocation;
	
	
	public ProxyExtensionMethodInvocation(InterceptorChain interceptorChain,MethodInvocation methodInvocation) {
		this.interceptorChain = interceptorChain;
		this.methodInvocation = methodInvocation;
	}
	
	@Override
	public Object[] getArguments() {
		return methodInvocation.getArguments();
	}

	@Override
	public Object proceed() throws Throwable {
		InterceptorChain nextChain = interceptorChain.getNextInterceptorChain();
		if(nextChain != null){
			return nextChain.getMethodConfig().getMethodInterceptor().invoke(new ProxyExtensionMethodInvocation(nextChain, methodInvocation));
		}
		return methodInvocation.proceed();
	}

	/**
	 * 根据key获取参数
	 * @param key
	 * @return
	 */
	public Object get(String key){
		return interceptorChain.getMethodConfig().getParam().get(key);
	}
	
	/**
	 * 获取所有的参数
	 * @param key
	 * @return
	 */
	public Object getParam(){
		return interceptorChain.getMethodConfig().getParam();
	}
	
	@Override
	public Object getThis() {
		return methodInvocation.getThis();
	}

	@Override
	public AccessibleObject getStaticPart() {
		return methodInvocation.getStaticPart();
	}

	@Override
	public Method getMethod() {
		return methodInvocation.getMethod();
	}
	
	public MethodInvocation getMethodInvocation(){
		return methodInvocation;
	}
}
